import Vue from 'vue'
import VueRouter from 'vue-router'
import Index from '../layout/index.vue'
import Login from '../views/Login'
import Dashboard from '../views/Dashboard'
import loca from '../utils/loca.js'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Index',
    component: Index,
    redirect: '/dashboard',
    children: [
      // 后台首页
      {
        path: '/dashboard',
        component: Dashboard
      },
      // 订单管理
      {
        path: '/orders',
        component: () =>
          import(/* webpackChunkName: "orders" */ '../views/Orders')
      },
      // 商品管理
      {
        path: '/goods',
        component: () =>
          import(/* webpackChunkName: "goods" */ '../views/Goods/'),
        children: [
          // 商品列表
          {
            path: '/goods/list',
            component: () =>
              import(/* webpackChunkName: "goods" */ '../views/Goods/List.vue')
          },
          // 商品添加
          {
            path: '/goods/add',
            component: () =>
              import(/* webpackChunkName: "goods" */ '../views/Goods/Add.vue')
          },
          // 商品分类
          {
            path: '/goods/cate',
            component: () =>
              import(/* webpackChunkName: "goods" */ '../views/Goods/Cate.vue')
          }
        ]
      },
      // 店铺管理
      {
        path: '/store',
        component: () =>
          import(/* webpackChunkName: "Store" */ '../views/Store')
      },
      // 账号管理
      {
        path: '/account',
        component: () =>
          import(/* webpackChunkName: "account" */ '../views/Account'),
        children: [
          // 账号列表
          {
            path: '/account/list',
            component: () =>
              import(
                /* webpackChunkName: "account" */ '../views/Account/List.vue'
              )
          },
          // 添加账号
          {
            path: '/account/add',
            component: () =>
              import(
                /* webpackChunkName: "account" */ '../views/Account/Add.vue'
              )
          },
          // 修改密码
          {
            path: '/account/cate',
            component: () =>
              import(
                /* webpackChunkName: "account" */ '../views/Account/Cate.vue'
              )
          },
          // 个人中心
          {
            path: '/account/mine',
            component: () =>
              import(
                /* webpackChunkName: "account" */ '../views/Account/Mine.vue'
              )
          }
        ]
      },
      // 销售统计
      {
        path: '/count',
        component: () =>
          import(/* webpackChunkName: "count" */ '../views/Count'),
        children: [
          // 商品统计
          {
            path: '/count/commodity',
            component: () =>
              import(
                /* webpackChunkName: "count" */ '../views/Count/Commodity.vue'
              )
          },
          // 订单统计
          {
            path: '/count/order',
            component: () =>
              import(/* webpackChunkName: "count" */ '../views/Count/Order.vue')
          }
        ]
      }
    ]
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '*',
    redirect: '/404'
  },
  {
    path: '/404',
    component: () => import(/* webpackChunkName: "error" */ '../views/Error')
  }
]
// component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
const router = new VueRouter({
  routes
})

// 使用导航守卫完成登录拦截
router.beforeEach((to, from, next) => {
  const token = loca.get('token')
  if (token) {
    next()
  } else {
    if (to.path === '/login') {
      next()
    } else {
      next('/login')
    }
  }
})

export default router
